﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Xml;
using System.Xml.XPath;
using System.Globalization;
using System.Threading;

namespace MipsPipelineSimulator
{
    class CommonTool
    {
        static public void ThreadDrawPaths(object param1)
        {
            DrawPaths param = (DrawPaths)param1;
            Control control = param.control;
            DrawTool.DrawPaths(param.animate,control);
        }

        static public void StartDrawPathsThread(object param1)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(CommonTool.ThreadDrawPaths));
            thread.IsBackground = true;
            thread.Start(param1);
        }
        private delegate void DrawPathDelegate(Graphics[] graphics, Point[] points, Pen pen);
        static public void DrawPath(Control control, Graphics[] graphics, Point[] points, Pen pen)
        {
            object[] tmp = { graphics, points, pen };
            control.Invoke(new DrawPathDelegate(DrawTool.DrawPath), tmp);
        }

        static public void DrawPath(Control control, DrawPath[] drawPaths)
        {
            foreach (DrawPath drawPath in drawPaths)
            {
                object[] tmp = { drawPath.graphics, drawPath.points, drawPath.pen };
                control.Invoke(new DrawPathDelegate(DrawTool.DrawPath), tmp);
            }
        }

        static public bool InitialPositions(string path)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            try
            {
				//初始化IFPosition
				for (int i = 0; i < 25; i++)
				{
                    string xpath = "/Position/IF/Point[@name='p" + (i + 1).ToString() + "']/X";
					XmlNode node = doc.SelectSingleNode(xpath);
					int tmp = 0;
					if (int.TryParse(node.InnerText, out tmp))
					{
						IFStage.position[i].X = tmp;
					}
					else
					{
						throw new Exception("Error");
					}
                    xpath = "/Position/IF/Point[@name='p" + (i + 1).ToString() + "']/Y";
					node = doc.SelectSingleNode(xpath);
					if (int.TryParse(node.InnerText, out tmp))
					{
						IFStage.position[i].Y = tmp;
					}
					else
					{
						throw new Exception("Error");
					}
				}
                //初始化IDPosition
                for (int i = 0; i < 42; i++)
                {
                    string xpath = "/Position/ID/Point[@name='p" + (i + 1).ToString() + "']/X";
                    XmlNode node = doc.SelectSingleNode(xpath);
                    int tmp = 0;
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        IDStage.position[i].X = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                    xpath = "/Position/ID/Point[@name='p" + (i + 1).ToString() + "']/Y";
                    node = doc.SelectSingleNode(xpath);
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        IDStage.position[i].Y = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                }
                //初始化EXPosition
                for (int i = 0; i < 20; i++)
                {
                    string xpath = "/Position/EX/Point[@name='p" + (i + 1).ToString() + "']/X";
                    XmlNode node = doc.SelectSingleNode(xpath);
                    int tmp = 0;
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        EXStage.position[i].X = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                    xpath = "/Position/EX/Point[@name='p" + (i + 1).ToString() + "']/Y";
                    node = doc.SelectSingleNode(xpath);
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        EXStage.position[i].Y = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                }
                //初始化MEMPosition
                for (int i = 0; i < 15; i++)
                {
                    string xpath = "/Position/MEM/Point[@name='p" + (i + 1).ToString() + "']/X";
                    XmlNode node = doc.SelectSingleNode(xpath);
                    int tmp = 0;
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        MEMStage.position[i].X = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                    xpath = "/Position/MEM/Point[@name='p" + (i + 1).ToString() + "']/Y";
                    node = doc.SelectSingleNode(xpath);
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        MEMStage.position[i].Y = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                }
                //初始化WBPosition
                for (int i = 0; i < 12; i++)
                {
                    string xpath = "/Position/WB/Point[@name='p" + (i + 1).ToString() + "']/X";
                    XmlNode node = doc.SelectSingleNode(xpath);
                    int tmp = 0;
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        WBStage.position[i].X = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                    xpath = "/Position/WB/Point[@name='p" + (i + 1).ToString() + "']/Y";
                    node = doc.SelectSingleNode(xpath);
                    if (int.TryParse(node.InnerText, out tmp))
                    {
                        WBStage.position[i].Y = tmp;
                    }
                    else
                    {
                        throw new Exception("Error");
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        static public string NumToStr(TypeCode typeCode, object value, string baseMark, bool ifAlignment)
        {
            string tmp = null;
            switch (typeCode)
            {
                case TypeCode.Int32:
                    {
                        tmp = ((Int32)value).ToString(baseMark + (ifAlignment ? "8" : ""));
                        break;
                    }
                case TypeCode.UInt32:
                    {
                        tmp = ((UInt32)value).ToString(baseMark + (ifAlignment ? "8" : ""));
                        break;
                    }
                case TypeCode.Int64:
                    {
                        tmp = ((Int64)value).ToString(baseMark + (ifAlignment ? "16" : ""));
                        break;
                    }
                case TypeCode.UInt64:
                    {
                        tmp = ((UInt64)value).ToString(baseMark + (ifAlignment ? "16" : ""));
                        break;
                    }
            }
            return tmp;
        }

        static public object StrToNum(TypeCode typeCode, string str, int baseMark)
        {
            object value = null;
            switch (typeCode)
            {
                case TypeCode.Int32:
                    {
                        value = Convert.ToInt32(str, baseMark);
                        break;
                    }
            }
            return value;
        }
    }
}
